package com.rk.kbk.controller;

import com.rk.kbk.common.JsonResp;
import com.rk.kbk.dao.CallSettingDao;
import com.rk.kbk.dto.PageDto;
import com.rk.kbk.model.CallSetting;
import com.rk.kbk.model.WithdrawSetting;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.math.BigInteger;
import java.util.List;

/**
 * @CallSettingController
 * @Controller
 * @version : Ver 1.0
 */
@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/callSetting")
public class CallSettingController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private CallSettingDao callSettingDao;
    @PersistenceContext
    private EntityManager entityManager;

    /**
     * @添加/修改
     * @param callSetting
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addOrUpdateCallSetting",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addOrUpdateCallSetting(@RequestBody CallSetting callSetting){
        log.debug("添加/修改");
        callSettingDao.save(callSetting);
        return JsonResp.ok(callSetting);
    }

    /**
     * @批量添加/修改
     * @param callSettings
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addOrUpdateSomeCallSetting",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addOrUpdateSomeCallSetting(@RequestBody List<CallSetting> callSettings){
        log.debug("批量添加/修改");
        callSettingDao.save(callSettings);
        return JsonResp.ok();
    }

    /**
     * @获取详情-ByCallSettingId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/callSettingDetails",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findCallSettingDetailsByCallSettingId() {
        log.debug("获取详情-ByCallSettingId");
        return JsonResp.ok(callSettingDao.findAll());
    }

    /**
     * @分页查询(jpa方式)
     * @param pageNum 第几页
     * @param pageSize 每页的条数
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/jpa/page",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findPageCallSettingJpa( Integer pageNum, Integer pageSize) {
        log.debug("分页查询(jpa方式)");
        //多条件排序Sort
        //Sort sort = new Sort(Sort.Direction.DESC, "createdate").and(new Sort(Sort.Direction.AES, "id"));
        //排序Sort
        Sort sort = new Sort(Sort.Direction.DESC, "gmtDatetime");
        Pageable pageable = new PageRequest(pageNum-1, pageSize, sort);
        Page page = null;
            page = callSettingDao.findAll(pageable); //无条件分页
        return JsonResp.ok(new PageDto(page));
    }

    /**
     * @删除-one(永久)
     * @param callSettingId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteCallSettingOne",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteCallSettingOne(Long callSettingId) {
        log.debug("删除-one(永久)");
        callSettingDao.delete(callSettingId);
        return JsonResp.ok();
    }

    /**
     * @批量删除(永久)
     * @param callSettings
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteSomeCallSetting",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteSomeCallSetting(@RequestBody List<CallSetting> callSettings) {
        log.debug("批量删除(永久)");
        callSettingDao.delete(callSettings);
        return JsonResp.ok();
    }

    /**
     * @修改状态
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/updateStatus",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp updateStatus(Long id) {
        log.debug("修改状态");
        CallSetting c=callSettingDao.findOne(id);
        if(c.getStatus()==0){
            c.setStatus(1);
        }else{
            c.setStatus(0);
        }
        callSettingDao.save(c);
        return JsonResp.ok();
    }

}
